{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第四次练习"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "+ 请务必交到exer4文件夹下，谢绝交到master下\n",
    "+ 请不要改动任何文件，拜托\n",
    "+ 请在10月30日前提交。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "请写一下姓名和学号：\n",
    "+ 姓名  姚迈\n",
    "+ 学号  0165186"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目一\n",
    "一般的歌，分为主歌和副歌部分。副歌有重复，对比两大功能。这就是A－B的基本曲式形式结构。  \n",
    "请找一首你的主打歌，把歌词打印出来，重复的副歌部分，请写一个函数，调用函数打印出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "书里总爱写到喜出望外的傍晚\n",
      "骑的单车还有他和她的对谈\\女孩的白色衣裳男孩爱看她穿\n",
      "好多桥段\n",
      "好多都浪漫\n",
      "好多人辛酸\n",
      "好聚好散\n",
      "慢慢喜欢你\n",
      "慢慢的亲密\n",
      "慢慢聊自己\n",
      "慢慢和你走在一起\n",
      "慢慢我想配合你\n",
      "慢慢把我给你\n",
      "慢慢喜欢你\n",
      "慢慢的回忆\n",
      "慢慢的陪你慢慢的老去\n",
      "因为慢慢是个最好的原因\n",
      "慢慢喜欢你\n",
      "慢慢的亲密\n",
      "慢慢聊自己\n",
      "慢慢和你走在一起\n",
      "慢慢我想配合你\n",
      "慢慢把我给你\n",
      "慢慢喜欢你\n",
      "慢慢的回忆\n",
      "慢慢的陪你慢慢的老去\n",
      "因为慢慢是个最好的原因\n"
     ]
    }
   ],
   "source": [
    "print('书里总爱写到喜出望外的傍晚\\n骑的单车还有他和她的对谈\\女孩的白色衣裳男孩爱看她穿\\n好多桥段\\n好多都浪漫\\n好多人辛酸\\n好聚好散')\n",
    "def voice():\n",
    "    print('慢慢喜欢你\\n慢慢的亲密\\n慢慢聊自己\\n慢慢和你走在一起\\n慢慢我想配合你\\n慢慢把我给你\\n慢慢喜欢你\\n慢慢的回忆\\n慢慢的陪你慢慢的老去\\n因为慢慢是个最好的原因')\n",
    "voice()\n",
    "voice()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目二\n",
    "请写一个函数，能够计算均值、样本方差和n(自己写公式哦)。\n",
    "每次输入十个以上的数字，能够输出上述三个统计量。\n",
    "至少调用两次。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有10个数\n",
      "均值是:11.5\n",
      "样本方差是:145.85000000000002\n",
      "一共有13个数\n",
      "均值是:6.0\n",
      "样本方差是:14.769230769230766\n"
     ]
    }
   ],
   "source": [
    "def statistics( *arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in arg:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"一共有%s个数\" % count)\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in arg:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    print(\"样本方差是:%s\" % var)\n",
    "statistics(45,5,3,4,6,6,7,8,20,11)\n",
    "statistics(4,3,5,9,7,13,1,4,0,6,8,5,13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目三\n",
    "刚才那个函数，默认的是输出均值、样本方差和n，请在后面添加一个关键词参数，让这个函数可以选择输出样本标准差或方差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "一共有11个数\n",
      "均值是:6.0\n",
      "1\n",
      "样本标准差是：3.3166247903554\n",
      "一共有12个数\n",
      "均值是:7.333333333333333\n",
      "1\n",
      "样本标准差是：5.1404515778521285\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "##输入0显示方差\n",
    "def statistics(a,*arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    for value in arg:\n",
    "        count+=1\n",
    "        sum+=value\n",
    "    ave=sum/count\n",
    "    print(\"一共有%s个数\" % count)\n",
    "    print(\"均值是:%s\" % ave)\n",
    "    sumsquare=0\n",
    "    for value in arg:\n",
    "        sumsquare+=value**2\n",
    "    var=sumsquare/count-ave**2\n",
    "    samplevar=var*count/(count-1)\n",
    "    a=int(input())    \n",
    "    if a==0:\n",
    "        print(\"样本方差是:%s\" % samplevar)\n",
    "    else:\n",
    "        varstd=math.sqrt(samplevar)\n",
    "        print(\"样本标准差是：%s\" %varstd )\n",
    "statistics(0,1,2,3,4,5,6,7,8,9,10,11)\n",
    "statistics(2,3,5,9,7,13,1,4,0,8,8,17,13)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 题目四\n",
    "刚才那个函数，请再增加三个关键词参数，让这个参数可以选择输出均值的标准误、最大值、最小值或不输出这三个统计量。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本中最大值是：5\n",
      "样本中最小值是：2\n",
      "均值标准误为：0.7602631123499272\n",
      "\n",
      "\n",
      "样本中最大值是：11\n",
      "样本中最小值是：5\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "def statistics(meanerror=None,max=None,min=None,*arg):\n",
    "    count=0\n",
    "    sum=0\n",
    "    nsquare=0\n",
    "    if max:\n",
    "        for value in arg:\n",
    "            if value>max:\n",
    "                max=value\n",
    "        print(\"样本中最大值是：%s\" % max)\n",
    "    if min:\n",
    "        for value in arg:\n",
    "            min=value\n",
    "        for value in arg:\n",
    "            if value<min:\n",
    "                min=value\n",
    "        print(\"样本中最小值是：%s\" % min)\n",
    "    if meanerror:\n",
    "        for value in arg:\n",
    "            count+=1\n",
    "            sum+=value\n",
    "        ave=sum/count\n",
    "        sumsquare=0\n",
    "        for value in arg:\n",
    "            sumsquare+=value**2\n",
    "        var=sumsquare/count-ave**2\n",
    "        samplevar=var*count/(count-1)\n",
    "        nsquare=math.sqrt(count)\n",
    "        print(\"均值标准误为：%s\" % (samplevar/nsquare))  \n",
    "statistics(1,1,1,2,2,3,4,5)\n",
    "print(\"\\n\")\n",
    "statistics(0,1,2,5,11,9,6,7)\n",
    "statistics(0,0,0,4,6,8)\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
